package com.example.examapp.controller;

import com.example.examapp.enums.Role;
import com.example.examapp.model.User;
import com.example.examapp.service.UserService;
import org.lzx.base.common.Result;
import org.lzx.base.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.stream.Collectors;


/**
 * @version v1.0
 * @package com.example.examapp.controller
 * @data 2022/12/5 20:29
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/import")
    public Result importExecel(@RequestParam("file")MultipartFile file) throws IOException {
        return ResultUtil.success(userService.importFormExcel(file));

    }

    @GetMapping("/list")
    public Result list(){
        return ResultUtil.success(userService.queryAll()
                .stream().filter(e->e.getRole().equals(Role.STUDENT)).collect(Collectors.toList()));
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id")String id){
        return ResultUtil.success(userService.del(id));

    }

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        return  ResultUtil.success(userService.login(user.getUserName(),user.getPwd())!=null);
    }
}
